<template>
  <div class="app-container">
    <el-card>
      <el-form inline>
        <el-form-item label="模型名称">
          <el-input v-model="keyword" placeholder="模型名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="getList()"
            >搜索</el-button
          >
          <el-button
            size="mini"
            @click="
              () => {
                keyword = '';
                getList();
              }
            "
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="open_add_dialog" size="mini"
        >添加</el-button
      >
      <el-table :data="list">
        <el-table-column
          v-for="item in list_c"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column label="操作" v-slot="{ row }">
          <el-button type="primary" size="mini" @click="open_edit_dialog(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini">删除</el-button>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="
          (val) => {
            this.pager.limit = val;
            this.getList();
          }
        "
        @current-change="
          (val) => {
            this.pager.page = val;
            this.getList();
          }
        "
        :current-page="pager.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      @close="reset_form"
      center
    >
      <el-form :model="list_e" ref="list_e_ref" inline>
        <el-form-item prop="product" label="产品" required>
          <el-input v-model="list_e.product" placeholder="产品"></el-input>
        </el-form-item>
        <el-form-item prop="productModel" label="产品型号">
          <el-input
            v-model="list_e.productModel"
            disabled
            placeholder="产品型号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="productSpecification" label="产品规格">
          <el-input
            v-model="list_e.productSpecification"
            disabled
            placeholder="产品规格"
          ></el-input>
        </el-form-item>
        <el-form-item prop="modelName" label="模型名称" required>
          <el-input
            v-model="list_e.modelName"
            placeholder="产品规格"
          ></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input
            type="textarea"
            v-model="list_e.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            @click="open_add_material_dialog"
            >选择</el-button
          >
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-table :data="list_e.detailList">
        <el-table-column label="序号" v-slot="{ $index }">{{
          $index + 1
        }}</el-table-column>
        <el-table-column width="200px" v-slot="{ row }" label="物料">
          <span>{{ row.material.name }}</span>
          <el-button
            style="margin-left: 20px"
            type="primary"
            size="mini"
            @click="open_edit_material_dialog(row.id)"
            >选择</el-button
          >
        </el-table-column>
        <el-table-column
          v-for="item in detailList_c"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column width="200px" label="操作" v-slot="{ $index }">
          <el-button
            size="mini"
            type="success"
            @click="
              () => {
                list_e.detailList.splice($index + 1, 0, {
                  material: {},
                  id: Math.floor(Math.random() * 10000),
                });
              }
            "
            >添加</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="
              () => {
                list_e.detailList.splice($index, 1);
              }
            "
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <template #footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
    <el-dialog
      :visible="visible_material"
      title="选择物料档案"
      @close="visible_material = false"
      center
      width="1200px"
    >
      <el-row>
        <el-col :span="6">
          <el-tree
            :default-expand-all="true"
            :data="list_material_category"
            :props="{ label: 'name' }"
            @node-click="
              (val) => {
                this.material_category_id = val.pid;
                this.getMaterialList();
              }
            "
          ></el-tree>
        </el-col>
        <el-col :span="18">
          <el-table :data="list_material">
            <el-table-column
              v-for="item in list_material_c"
              :key="item.prop"
              :label="item.label"
              :prop="item.prop"
            ></el-table-column>
            <el-table-column label="操作" v-slot="{ row }">
              <el-button
                size="mini"
                type="primary"
                @click="add_detailList_row(row)"
                >选择</el-button
              >
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getProductModelingList, getMaterialList, getMaterialCategoryList, editProductModeling, addProductModeling } from '@/api_zc/mes/product-modeling'
export default {
  mounted () {
    this.getList()
    this.getMaterialList()
    this.getMaterialCategoryList()
  },
  data () {
    return {
      list: [],
      list_c: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '模型名称',
          prop: 'modelName'
        },
        {
          label: '产品',
          prop: 'product'
        },
        {
          label: '产品型号',
          prop: 'productModel'
        },
        {
          label: '产品规格',
          prop: 'productSpecification'
        },
        {
          label: '产品单位',
          prop: 'productUnit'
        },
      ],
      list_e: {
        id: null,
        product: null,
        productModel: null,
        productSpecification: null,
        modelName: null,
        remark: null
      },
      detailList_c: [
        {
          label: '型号',
          prop: 'material.model'
        },
        {
          label: '规格',
          prop: 'material.specification'
        },
        {
          label: '单位',
          prop: 'material.unit'
        },
      ],
      keyword: '',
      pager: {
        page: 1,
        limit: 10,
        total: 0
      },
      visible: false,
      title: '',
      is_add: false,
      visible_material: false,
      list_material: [],
      list_material_c: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '物料编号',
          prop: 'code'
        },
        {
          label: '物料名称',
          prop: 'name'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '单价',
          prop: 'price'
        },
        {
          label: '类型',
          prop: 'category.name'
        },
        {
          label: '备注',
          prop: 'remark'
        },
      ],
      list_material_category: [],
      material_category_id: 0,
      is_add_material: false,
      edit_material_id: 0
    }
  },
  methods: {
    async getList () {
      const { data: { total, content } } = await getProductModelingList({ page: this.pager.page, limit: this.pager.limit, keyword: this.keyword })
      this.list = content
      this.pager.total = total
    },
    async getMaterialList () {
      const { data: { content } } = await getMaterialList({ pid: this.material_category_id })
      this.list_material = content
    },
    async getMaterialCategoryList () {
      const { data } = await getMaterialCategoryList()
      this.list_material_category = data
    },
    open_edit_dialog (row) {
      this.is_add = false
      this.visible = true
      this.title = '修改产品建模'
      this.$nextTick(() => {
        Object.assign(this.list_e, row)
      })
    },
    open_add_dialog () {
      this.is_add = true
      this.visible = true
      this.title = '添加产品建模'
    },
    reset_form () {
      this.$refs.list_e_ref.resetFields()
      this.list_e.detailList = []
    },
    open_add_material_dialog () {
      this.visible_material = true
      this.is_add_material = true
    },
    open_edit_material_dialog (id) {
      this.visible_material = true
      this.is_add_material = false
      this.edit_material_id = id
    },
    add_detailList_row (row) {
      if (this.is_add_material) {
        row.category = null
        this.list_e.detailList.push({
          material: row
        })
      } else {
        row.category = null
        this.list_e.detailList.find(item => item.id == this.edit_material_id).material = row
      }
      this.visible_material = false
    },
    submit () {
      this.$refs.list_e_ref.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请完善信息')
        }
        if (this.is_add) {
          await addProductModeling(this.list_e)
          this.$message.success('添加成功')
        } else {
          await editProductModeling(this.list_e)
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
      })
    }
  }
}
</script>

<style>
</style>